var app = angular.module('JiangGanApp')
app.controller('RegionCtrl', ['$scope','$state','DiagramService','$rootScope','$http','$anchorScroll','DataService',RegionCtrl]);


function RegionCtrl($scope,$state,DiagramService,$rootScope,$http,$anchorScroll,DataService){

	$rootScope.showMap = false;
	$scope.goMap = function () {
		$state.go('app.region.map');
	}

	$scope.goDetail=function(id){
		var url = $state.href('app.netbar.detail.info',{id:id});
		window.open(url,'_blank');
	}


	$scope.pageOption = {
	    pageSize:10,
	    itemCount:12  
  	}

  	$scope.haveType = function(val){
		if($scope.option.type.indexOf(val)>-1){
			return true;
		}else{
			return false;
		}
	}

	$scope.checkType = function(val){
		var index = $scope.option.type.indexOf(val);
		if(index==-1){
			$scope.option.type.push(val)
		}else{
			$scope.option.type.splice(index,1)
		}
	}

	$scope.option = {
		'type':['1', '2', '3', '4'],
		'info':'',
		'pj':'',
		'cy':'false'
	}

	var commListY = [];
	getAllSQ();
	function getAllSQ() {
		var today = new Date('2017-08-13');
		// today.setDate(today.getDate() - 1);
		var type = ['边缘性场所','流动人口','黄赌毒举报','人员触网','打防控案情','110警情'];
		DataService.restGetApi('area_totals?filter={"where":{"date":"' + today.format('yyyy-MM-dd') + '"},"order":"total_score desc"}')
		.then(function success (data) {
			commListY = [];
			for(var i = 0; i < data.length; i++) {
				var item = {
					id: i,
					name: data[i].community,
					df: data[i].total_score,
					date: data[i].date,
					ms: '',
					dj: '',
					type: type,
					data: [data[i].enter_score, data[i].flow_score, data[i].hdd_score, data[i].net_score, data[i].dfk_score, data[i].jq_score]
				}
				if(data[i].jq_score > 19) {
					item.ms += '警情得分高，高发；';
				}
				if(data[i].dfk_score > 19) {
					item.ms += '案件得分高，高发；';
				}
				if(data[i].flow_score > 15) {
					item.ms += '流动人口较多；';
				}
				if(data[i].net_score > 15) {
					item.ms += '网吧旅馆触网人数较多；';
				}
				if(data[i].hdd_score > 15) {
					item.ms += '黄赌毒案件高发；';
				}
				if(data[i].enter_score > 15) {
					item.ms += '边缘性场所较多；';
				}

				if(data[i].total_score > 70) {
					item.dj = '高危';
				}
				else if(data[i].total_score > 50) {
					item.dj = '重点';
				}
				else if(data[i].total_score > 30) {
					item.dj = '欠安';
				}
				else {
					item.dj = '正常';
				}
				commListY.push(item);
			}
			generateData([1, 10]);
		})
	}

	function generateData(item) {
		$scope.commConfig.dataLoaded = false;
		$scope.pageOption.itemCount = commListY.length;
		$scope.commList = commListY.slice(item[0]-1,item[1]);
		$scope.commOption = []
		for(var i = 0 ; i<$scope.commList.length;i++){
			$scope.commOption.push(DiagramService.getCommBar($scope.commList[i].type,$scope.commList[i].data,30))
		}
		$scope.commConfig.dataLoaded = true;
	}


	$scope.commList = [];

	$scope.getData = function(item){
		if(item) {
			generateData(item);
		}
  	
	}
	
	$scope.commConfig =  {
	    theme:'default',
	    dataLoaded:false
	};
	$scope.commOption = []

	//灌入
	for(var i = 0 ; i<$scope.commList.length;i++){
		$scope.commOption.push(DiagramService.getCommBar($scope.commList[i].type,$scope.commList[i].data,10))
	}



	$scope.goXq=function(name, date, id){
	  var url = $state.href('app.region.details.briefinfo',{name:name, id: id});
	  window.open(url,'_blank');
	}

}
